package org.newcih.activiti.activitiInAction.chapter5;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.junit.Test;
import org.newcih.dao.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static junit.framework.TestCase.assertNotNull;

public class IdentifyServiceTest extends BaseTest {

//    @Rule
//    public ActivitiRule activitiRule = new ActivitiRule();

    @Autowired
    private IdentityService identityService;

    @Test
    public void testUser() throws Exception {
//        IdentityService identityService = activitiRule.getIdentityService();
        // 创建用户对象
        User user = identityService.newUser("newcih");
        user.setFirstName("CIH");
        user.setLastName("NEW");
        user.setEmail("newcih@foxmail.com");
        // 保存到数据库
        identityService.saveUser(user);

        // 验证是否保存成功
        User userInDB = identityService.createUserQuery().userId("newcih").singleResult();

        assertNotNull(userInDB);

        // 删除用户
        identityService.deleteUser("newcih");

        // 验证删除结果
        userInDB = identityService.createUserQuery().userId("newcih").singleResult();
        assertNull(userInDB);
    }

    @Test
    public void testGroup() {
        Group group = identityService.newGroup("cih");
        group.setName("Teacher Group");
        group.setType("assignment");

        identityService.saveGroup(group);

        List<Group> groupList = identityService.createGroupQuery().groupId("cih").list();
        assertEquals(1, groupList.size());

        identityService.deleteGroup("cih");

        groupList = identityService.createGroupQuery().groupId("cih").list();

        assertEquals(0, groupList.size());
    }
}
